/**
 * 
 */
package vue.desktop;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.cnf.CNF;
import model.session.CNFUse;

/**
 * Show the basic information about a CNF.
 * 
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGDeskCNFInformation extends JPanel {

	private static final long	serialVersionUID		= 1L;

	public static final int		MAX_SIZE_NAME			= 15;

	public static final String	DEFAULT_STRING			= "UNKNOW";
	public static final String	DEFAULT_CLAUSE_PREFIX	= "#Clause : ";
	public static final String	DEFAULT_VAR_PREFIX		= "#Variable : ";
	public static final Color	FIRST					= new Color(244, 164, 96, 200);
	public static final Color	SAT						= new Color(152, 251, 152, 200);
	public static final Color	UNSAT					= new Color(220, 20, 60, 200);
	public static final Color	IRRELEVANT				= new Color(0, 0, 0, 179);
	public static final Color	IRRELEVANT_FORGROUND	= new Color(255, 255, 255, 179);
	public static final Color	TO_BE_DETERMINED		= new Color(200, 200, 200, 200);
	public static final Color	INFO					= new Color(240, 230, 140, 100);


	private JLabel				name;
	private JLabel				clause;
	private JLabel				variable;


	/**
	 * Create a {@link IGDeskCNFInformation}.
	 * 
	 * @param cnfuse
	 *            cnf to display.
	 */
	public IGDeskCNFInformation(CNFUse cnfuse) {
		setLayout(new GridLayout(3, 1));

		// CNF cnf = cnfuse.getCnf();

		// name
		name = new JLabel(DEFAULT_STRING, JLabel.CENTER);
		name.setOpaque(true);
		name.setBackground(TO_BE_DETERMINED);
		name.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		// name.setFont(Font.gFont.BOLD);
		// name.set

		clause = new JLabel(DEFAULT_CLAUSE_PREFIX + DEFAULT_STRING, JLabel.CENTER);
		clause.setBackground(INFO);
		clause.setOpaque(true);
		clause.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.BLACK));

		variable = new JLabel(DEFAULT_VAR_PREFIX + DEFAULT_STRING, JLabel.CENTER);
		variable.setBackground(INFO);
		variable.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		variable.setOpaque(true);

		add(name);
		add(clause);
		add(variable);
		refresh(cnfuse);

		setOpaque(false);
		// setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
		// setBorder(BorderFactory.createLineBorder(Color.BLACK));

	}

	/**
	 * Refresh the information.
	 * 
	 * @param cnfuse
	 *            cnf to display.
	 */
	public void refresh(CNFUse cnfuse) {
		CNF cnf = cnfuse.getCnf();

		if (cnfuse.getFirst() == cnfuse)
		{
			name.setBackground(FIRST);
			switch (cnfuse.getStatus()) {
			case PROVED_SAT:
				name.setForeground(SAT);
				break;
			case PROVED_UNSAT:
				name.setForeground(UNSAT);
				break;
			case TO_BE_DETERMINED:
				name.setForeground(Color.BLACK);
				break;
			}
		}
		else
			switch (cnfuse.getStatus()) {
			case PROVED_SAT:
				name.setForeground(Color.BLACK);
				name.setBackground(SAT);
				break;
			case PROVED_UNSAT:
				name.setForeground(Color.BLACK);
				name.setBackground(UNSAT);
				break;
			case TO_BE_DETERMINED:
				name.setForeground(Color.BLACK);
				name.setBackground(TO_BE_DETERMINED);
				break;
			case IRRELEVANT:
				name.setBackground(IRRELEVANT);
				name.setForeground(IRRELEVANT_FORGROUND);
				break;
			}

		String tmpname;
		if (cnf.getLastExportFile() != null)
			tmpname = cnf.getLastExportFile().getName();
		else
			tmpname = cnf.getCNFfile().getName();

		if (tmpname.length() > MAX_SIZE_NAME)
			tmpname = tmpname.substring(0, MAX_SIZE_NAME - 3) + "...";

		name.setText(tmpname);

		clause.setText(DEFAULT_CLAUSE_PREFIX + cnf.getDeclaredNumClause());
		variable.setText(DEFAULT_VAR_PREFIX + cnf.getDeclaredNumVar());
	}

}
